{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# numpy\n",
    "## numpy简介\n",
    " Python库，多维度数组的计算，提供大量的库函数，辅助进行矩阵计算。完成的计算任务主要包括\n",
    "1. 机器学习模型：\n",
    "  2. *编写机器学习算法的时候，需要对矩阵进行大量的计算。包括矩阵乘法，换位，加法等。*\n",
    "  2. *Numpy数组，用于存储训练数据，存储机器学习模型参数*\n",
    "1. 图像处理和计算机图形学：\n",
    "  2. *图像表示为多维度的数字数组，numpy提供了优秀的库函数处理图像，如镜像图像，旋转\n",
    "1. 数学任务：\n",
    "  2. *应度数学计算，数值积分，微分，内插，外推，基本可替代matlab*\n",
    "  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## numpy数组\n",
    "NumPy提供的最重要的数据结构是一个称为NumPy数组的强大对象。\n",
    "NumPy数组是通常的Python数组的扩展。NumPy数组配备了大量的函数和运算符numpy\n",
    "定义numpy数组\n",
    "### define array np.array\n",
    "~~~python\n",
    "import numpy as np \n",
    "my_array = np.array([1, 2, 3, 4, 5]) \n",
    "print my_array\n",
    "~~~"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "my_array = np.array([1,2,3,4,5])\n",
    "print(my_array)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 二维数组 列表中的列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "(2, 3)\n"
     ]
    }
   ],
   "source": [
    "a = np.array([(1,2,3),(4,5,6)])\n",
    "print(a)\n",
    "print(a.shape) # 两行三列"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数组加1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 3 4]\n",
      " [5 6 7]]\n"
     ]
    }
   ],
   "source": [
    "print(a+1) # 数组中的所有元素加1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数组乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  4  9]\n",
      " [16 25 36]]\n"
     ]
    }
   ],
   "source": [
    "print(a*a) # 数组对应的元素相乘"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数组的属性（对象，方法）\n",
    "   1. .shape 查看 数组维度\n",
    "   1. .dtype 查看数组类型 且numpy的数组中的数据类型必须是一致的 同时作为数组的类型的定义参数\n",
    "   1. .itemsize 查看元素字节\n",
    "   1. .size 查看数组中的元素个数\n",
    "   1. .ndim 查看数组维度信息\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### numpy 支持的数据类型 可以通过 .dtype作为参数指定数据类型\n",
    "~~~python\n",
    "    a = np.array([1,2,3,\"123\"], dtype=int) # 通过参数设定np的类型\n",
    "~~~\n",
    "~~~python\n",
    "    bool, int, float, str\n",
    "    np.float32, np.flaot64, np, np.int32, np.int6\n",
    "    \"float\",\"int\",\"float32\"\n",
    "~~~\n",
    "    python中通用的类型是object对象\n",
    "    当复杂的数据类型中,数组需要同时支持字符串,整形,以及数组,则可以通过将.dtype 设定为 dpyte = object 对象即可\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 '123' 'abc' list([1, 2])]\n",
      "<class 'numpy.ndarray'>\n",
      "object\n",
      "(6,)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.array([1,2,3,\"123\",\"abc\",[1,2]], dtype=object)\n",
    "print(a)\n",
    "print(type(a))  # 查看a的类型\n",
    "print(a.dtype)  # 查看a的数组类型\n",
    "print(a.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "my_array = np.array([(1,2,3),(4,5,6)])\n",
    "print(my_array.shape)  # 查看数组的维度\n",
    "print(my_array.dtype)  # 查看数据类型\n",
    "print(my_array.itemsize) # 查看数组中的元素的字节大小\n",
    "print(my_array.size)  # 查看数组中有多少个元素  可以直接作用于列表\n",
    "print(my_array.nbytes) # 查看数组总体的占字节量\n",
    "print(my_array.ndim)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数组的数据类型的转换 dtype 指定"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3]\n",
      "int32\n",
      "[1 2 3]\n",
      "[1. 2. 3.]\n",
      "[1. 2. 3.]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "b = np.array([1,2,3])\n",
    "print(b)\n",
    "print(b.dtype)\n",
    "c = np.array(b, dtype=\"float\") #会产生一个新的数组\n",
    "d = np.asarray(b, dtype=\"float\") # 不会生成一个新的数组\n",
    "b.astype(float)\n",
    "print(b)\n",
    "print(c)\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### np.inf 无穷大数组\n",
    "    在数组进行除以0的时候会得到警告"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ inf -inf  nan]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\bin\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:2: RuntimeWarning: divide by zero encountered in true_divide\n",
      "  \n",
      "C:\\Users\\bin\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in true_divide\n",
      "  \n"
     ]
    }
   ],
   "source": [
    "e = np.array([1,-1,0])\n",
    "print(e/0)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数组的生成\n",
    "#### 通过np.array 生成\n",
    "#### 通过np.zeros 生成全0数组\n",
    "#### 通过np,ones 全1 数组\n",
    "#### 通过np.arange 指定范围的数组 支持浮点数  指定开头,结尾,步长\n",
    "#### 通过np.linspace 指定生成指定范围的等线性数组 默认数组元素个数是50\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[0. 0. 0.]\n",
      "  [0. 0. 0.]\n",
      "  [0. 0. 0.]]\n",
      "\n",
      " [[0. 0. 0.]\n",
      "  [0. 0. 0.]\n",
      "  [0. 0. 0.]]]\n",
      "[[[1. 1. 1. 1.]\n",
      "  [1. 1. 1. 1.]]\n",
      "\n",
      " [[1. 1. 1. 1.]\n",
      "  [1. 1. 1. 1.]]]\n",
      "[1 3 5 7 9]\n",
      "[ 1.          2.83333333  4.66666667  6.5         8.33333333 10.16666667\n",
      " 12.        ]\n"
     ]
    }
   ],
   "source": [
    "aa = np.zeros((2,3,3))  # 生成指定维度的0数组\n",
    "print(aa)\n",
    "bb = np.ones((2,2,4))  # 生成指定维度的全1数组\n",
    "print(bb)\n",
    "cc = np.arange(1,10,2)  # 生成指定步长的一位数组 不包括结尾\n",
    "print(cc)\n",
    "dd = np.linspace(1.,12.,7) # 生成指定个数的一维数组\n",
    "print(dd)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数组的索引\n",
    "### 一维数组索引,切片\n",
    "### 多维度数组索引,切片\n",
    "    注:索引包含前面起始位置,但是不包含数组的索引的结束位置"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 一维数组索引切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "[2 4]\n"
     ]
    }
   ],
   "source": [
    "ee = np.array([1,2,3,4])\n",
    "print(ee[2]) # 索引得到数组元素值\n",
    "print(ee[1::2]) # 切片得到数组的一部分,仍然是数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 多维度数组的shape的变换 索引,切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]\n",
      "[[ 0  1  2  3  4]\n",
      " [ 5  6  7  8  9]\n",
      " [10 11 12 13 14]\n",
      " [15 16 17 18 19]]\n",
      "0\n",
      "1\n",
      "[ 1  6 11 16]\n",
      "[[ 0  1  2  3  4]\n",
      " [ 5  6  7  8  9]\n",
      " [10 11 12 13 14]\n",
      " [15 16 17 18 19]]\n",
      "[[ 6  7  8]\n",
      " [11 12 13]]\n"
     ]
    }
   ],
   "source": [
    "ff = np.arange(20)\n",
    "print(ff) # 生成一维数组\n",
    "ff.shape=(4,5) # 对数组进行shape的切换 \n",
    "print(ff)\n",
    "print(ff[0][0]) # 二维数组的索引\n",
    "print(ff[0,1])  # 二维的数组缩影方式2 索引0行1列的值\n",
    "print(ff[:,1])  # 取所有行的第一列元素\n",
    "print(ff[:,:])  # 取数组的所有行列元素  \n",
    "print(ff[1:3,1:4])  # 取 1,2 行的 1,2,3列 注意元素索引要点"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数组的迭代查找 通过for循环输出行 或者 np.ndenumerate枚举"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4]\n",
      "<class 'numpy.ndarray'>\n",
      "[5 6 7 8 9]\n",
      "<class 'numpy.ndarray'>\n",
      "[10 11 12 13 14]\n",
      "<class 'numpy.ndarray'>\n",
      "[15 16 17 18 19]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "for i in ff:\n",
    "    print(i)\n",
    "    print(type(i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 0) 0\n",
      "(0, 1) 1\n",
      "(0, 2) 2\n",
      "(0, 3) 3\n",
      "(0, 4) 4\n",
      "(1, 0) 5\n",
      "(1, 1) 6\n",
      "(1, 2) 7\n",
      "(1, 3) 8\n",
      "(1, 4) 9\n",
      "(2, 0) 10\n",
      "(2, 1) 11\n",
      "(2, 2) 12\n",
      "(2, 3) 13\n",
      "(2, 4) 14\n",
      "(3, 0) 15\n",
      "(3, 1) 16\n",
      "(3, 2) 17\n",
      "(3, 3) 18\n",
      "(3, 4) 19\n"
     ]
    }
   ],
   "source": [
    "for index, v in np.ndenumerate(ff):  # 通过np.ndenumerate枚举获取元素的索引位置以及元素值\n",
    "    print(index, v)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数组的操作\n",
    "    数组求和 .sum\n",
    "    数组求积, 数组积,内积,外积,向量积,点积"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数组元素求和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4]\n",
      " [ 5  6  7  8  9]\n",
      " [10 11 12 13 14]\n",
      " [15 16 17 18 19]]\n",
      "[30 34 38 42 46]\n",
      "[10 35 60 85]\n",
      "<built-in method sum of numpy.ndarray object at 0x000001BC136CC0D0>\n"
     ]
    }
   ],
   "source": [
    "aaa = np.arange(20)\n",
    "aaa.shape=(4,5)\n",
    "print(aaa)\n",
    "print(aaa.sum(axis=0))  # 求矩阵的列和\n",
    "print(aaa.sum(axis=1))  # 求矩阵的行和\n",
    "print(aaa.sum)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
